<template>
   <div class="app-container">
      <el-row :gutter="20">
         <!--用户数据-->
         <el-col :span="24" :xs="24">
            <el-form :model="queryParams"  ref="queryRef" :inline="true" label-width="90px" v-show="true"> 
                 <el-form-item label="分类代码" prop="code">
               <el-input  placeholder="请输入分类代码" v-model="form.code"/>
          </el-form-item>
               <el-form-item label="著作分类" prop="bookName">
                  <el-select
                        placeholder="请选择获奖等级"
                        clearable
                            style="width: 240px"
                            v-model="queryParams.bookGroup"
                            size="small"

                    >
                    <el-option
                        v-for="item in bookGroup"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
               </el-form-item>
               <el-form-item label="著作等级" prop="publicationName">
                 <el-select
                        placeholder="请选择获奖等级"
                        clearable
                            style="width: 240px"
                            v-model="queryParams.bookLevel"
                            size="small"

                    >
                    <el-option
                        v-for="item in bookLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
               </el-form-item>
               <el-form-item label="著作类型" prop="category">
                 <el-input
                     v-model="queryParams.bookType"
                     placeholder="请输入著作类型"
                     clearable
                     size="small"
                     style="width: 240px"
                  />
                  
          </el-form-item>
                <el-form-item>
                  <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" size="mini" @click="flushed">重置</el-button>
                  <el-button icon="Tickets" size="mini" @click="handleRef">参考</el-button>
               </el-form-item>
            </el-form>
            <el-row :gutter="10"  class="mb8" >
               <el-col :span="1.5">
                  <el-button
                     type="primary"
                     plain
                     icon="Plus"
                     size="mini"
                    @click="handleAdd"
                  >新增</el-button>
               </el-col>
               <el-col :span="1.5">
                  <el-button
                     type="success"
                     plain
                     icon="Delete"
                     size="mini"
                     :disabled="single"
                     @click="handleUpdate"
                  >修改</el-button>
               </el-col>
               <el-col :span="1.5">
                  <el-button
                     type="danger"
                     plain
                     icon="Delete"
                     size="mini"
                     :disabled="single"
                     @click="handleDelete"
                  >删除</el-button>
               </el-col>

            </el-row>
    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
           <el-table-column label="代码"  width="80" align="center" prop="code" />
          <el-table-column label="著作类型" align="center" prop="bookType" overflow:hidden  :show-overflow-tooltip="true" />
          <el-table-column label="著作分类" align="center" prop="bookGroup" width="90"/>
          <el-table-column label="著作等级" align="center" prop="bookLevel" width="90"/>
          <el-table-column label="工作量计算系数" align="center" prop="workloadStandard"  width="120" />
          <el-table-column label="备注" align="center" prop="remark"  width="100" />
          <el-table-column label="更新时间" align="center" prop="updatedTime" width="160" />
          <el-table-column label="创建时间" align="center" prop="createdTime" width="160"/>
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template #default="scope" >
               <el-button
                  v-if="scope.row.roleId !== 1"
                  type="text"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
               >修改</el-button>
               <el-button
                  v-if="scope.row.roleId !== 1"
                  type="text"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
               >删除</el-button>
            </template>
         </el-table-column>
      </el-table>
            <pagination
               v-show="total > 0"
               :total="total"
               v-model:page="queryParams.pageNum"
               v-model:limit="queryParams.pageSize"
               @pagination="getList"
            />
         </el-col>
      </el-row>

<!-- 参考表单对话框 -->
<el-dialog :title="title" v-model="open" width="650px" append-to-body>
    <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="number" label="序 号" width="80" align="center"/>
            <el-table-column prop="paper" label="著   作"  align="center" />
            <el-table-column prop="category" label="类别" width="100" align="center" />
            <el-table-column prop="workload" label="工作量" width="100" align="center" />
        </el-table>
</el-dialog>
<!-- 新增或删除表单对话框 -->
<el-dialog :title="title" v-model="addopen" width="430px" append-to-body>
      <el-form ref="bookRef" :model="form" :rules="rules" label-width="130px">
          <el-form-item label="分类代码" prop="code">
               <el-input  placeholder="请输入分类代码" v-model="form.code"/>
          </el-form-item>
        <el-form-item label="著作类型" prop="bookType">
          <el-input placeholder="请输入著作类型" v-model="form.bookType" />
        </el-form-item>
          <el-form-item label="著作等级" prop="bookLevel">
                    <el-select
                        placeholder="请选择著作等级"
                        clearable
                        style="width:  260px"
                        v-model="form.bookLevel"
                    >
                    <el-option
                        v-for="item in bookLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
        </el-form-item>
          <el-form-item label="工作量计算系数" prop="workloadStandard">
               <el-input-number  :min="0"
                                 placeholder="请输入工作量计算系数" 
                                 controls-position="right"
                                 v-model="form.workloadStandard"
                                 style="width: 260px"/>
          </el-form-item>
            <el-form-item label="备注">
             <el-input
               v-model="form.remark"
               autosize
               type="textarea"
               placeholder="请输入备注"
            /> </el-form-item>
       </el-form>
       <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel" >取 消</el-button>
            </div>
         </template>
</el-dialog>
   </div>
</template>

<script setup>

import { listAll , addDict , delDict , updateDict , getDict} from "@/api/nepu_dict/book";

 const total = ref(0);
 const bookList = ref([]);
 const dateRange = ref([]);
 const loading = ref(true);
 const title = ref("");
 const open = ref(false);
 const addopen = ref(false);
 const categoryValue = ref('')
 const levelValue =ref('')
 const ids = ref([]);
 const single = ref(true);
 const multiple = ref(true);
 const { proxy } = getCurrentInstance();
 const tableData = [
  {
    number: '1',
    paper: '专著（国家一级出版社）',
    category: 'A1',
    workload:'15/万字'
  },
  {
     number: '2',
    paper: '专著（国家二级出版社）',
    category: 'A2',
    workload:'10/万字'
  },
  {
    number: '3',
    paper: '译著（国家一级出版社）',
    category: 'A3',
    workload:'10/万字'
  },
  {
    number: '4',
    paper: '译著（国家二级出版社）',
    category: 'B1',
    workload:'6/万字'
  },
  {
    number: '5',
    paper: '编著（国家一级出版社）',
    category: 'A4',
    workload:'8/万字'
  },
  {
    number: '6',
    paper: '编著（国家二级出版社）',
    category: 'B2',
    workload:'5/万字'
  },
  {
    number: '7',
    paper: '国家级规划教材（教育部认定）',
    category: 'A5',
    workload:'35/万字'
  },
  {
    number: '8',
    paper: '教材（国家一级出版社）',
    category: 'A6',
    workload:'15/万字'
  },
  {
    number: '9',
    paper: '教材（国家二级出版社）',
    category: 'B3',
    workload:'12/万字'
  },
  {
    number: '10',
    paper: '校内实验指导书',
    category: 'C',
    workload:'2/万字'
  },
]
const bookLevel = ref([
        {value: 'A1',label: 'A1'},
        {value: 'A2',label: 'A2'},
        {value: 'A3',label: 'A3'},
        {value: 'A4',label: 'A4'},
        {value: 'A5',label: 'A5'},
        {value: 'B1',label: 'B1'},
        {value: 'B2',label: 'B2'},
        {value: 'B3',label: 'B3'},
        {value: 'B4',label: 'B4'},
        {value: 'C1',label: 'C1'},
        {value: 'C2',label: 'C2'},
        ])
const bookGroup = ref([
        {value: '著作',label: '著作',disabled:true},
        {value: '教材',label: '教材',disabled:true},
        ])
const data = reactive({
  form: {},
  queryParams: {
    order:'DESC',
    pageNum: 1,
    pageSize: 10,
    sortBy:'id',
    bookGroup:undefined,
    bookLevel: undefined,
    bookType: undefined,
  },
  rules: {
    //v-modle.number才可以
    workloadStandard: [
      { required: true, message: "不能为空", trigger: "blur" },
      { type: "number", min: 0,  message: "工作量不得小于0" },
    ],
    bookLevel:[
       {required: true, message: "不能为空", trigger: "blur"}
     ],
    bookType: [
      { required: true, message: "不能为空", trigger: "blur" },
      {
        min: 2,
        max: 50,
        message: "类型名称长度必须介于 2 和 50 之间",
        trigger: "change",
      },
    ],
    code:[
      {required: true, message: "不能为空", trigger: "blur" }
    ]
  },
});
const { queryParams, form , rules} = toRefs(data);

/** 查询列表 */
function getList(){
   loading.value = true;
   listAll(proxy.addDateRange(queryParams.value)).then(response => {
      bookList.value = response.content;
      total.value = response.totalElements;
      loading.value = false;
   })
}
  function resetQueryParams(){
        queryParams.value = {
             order:'DESC',
            pageNum: 1,
            pageSize: 10,
            sortBy:'id',
            bookGroup:undefined,
            bookLevel: undefined,
            bookType: undefined,
        }
    }

function flushed(){
  resetQueryParams()
  getList()
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const bookIds = row.id || ids.value;
  form.value.id = bookIds;
  getDict(bookIds).then(response => {
            form.value = response;
            addopen.value = true;
            title.value = "修改";
        });
}
function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.id);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }
/** 搜索按钮操作 */
function handleQuery(){
  queryParams.value.pageNum = 1
  getList()
}
/** 重置form表单 */
function reset() {
  form.value = {
    bookLevel: undefined,
    bookType: undefined,
    remark: undefined,
    workloadStandard: 0,
    code:undefined
   };
  proxy.resetForm("bookRef");
}
/** 确认操作 */
function submitForm() {
  proxy.$refs["bookRef"].validate(valid => {
    if (valid) {
        if (form.value.id != undefined) {
          updateDict(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          addopen.value = false;
          getList();
        });
        } 
        else {
        addDict(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          addopen.value = false;
          getList();
        });
      } 
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const bookIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除著作ID为"' + bookIds + '"的数据项?').then(function () {
    return delDict(bookIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 参考表单 */
function handleRef(){
 title.value='参考'
 open.value = true
}
/** 新增表单 */
function handleAdd(){
 reset()
 title.value = '录入信息'
 addopen.value = true
}
/** 取消按钮操作 */
function cancel() {
  open.value = false;
  addopen.value = false;
}
/** 分类选择 */
function categorySelect(){
    if(categoryValue.value === 'A1' || categoryValue.value === 'A2' ){levelValue.value = '专著'}
    if(categoryValue.value === 'A3' || categoryValue.value === 'B1' ){levelValue.value = '译著'}
    if(categoryValue.value === 'A4' || categoryValue.value === 'B2' ){levelValue.value = '编著'}
    if(categoryValue.value === 'A5'){levelValue.value = '国家级规划教材'}
    if(categoryValue.value === 'A6' || categoryValue.value === 'B3' ){levelValue.value = '教材'}
    if(categoryValue.value === 'C' ){levelValue.value = '校内实验指导书'}
}
/** 等级选择 */
function levelSelect(){
    if(levelValue.value === '4'){categoryValue.value = 'A5'}
    if(levelValue.value === '6'){categoryValue.value = 'C'}

}
getList()
</script>

<style>

</style>